/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 12/26/2010 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;
namespace DataAccessLayer
{
    /// <summary>
    /// Define ChiTietPhieuBanHangDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in ChiTietPhieuBanHangDao class , not here.
    /// </summary>
    public class ChiTietPhieuBanHangDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public ChiTietPhieuBanHangDaoBase() { }
        static ChiTietPhieuBanHangDaoBase()
        {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(ChiTietPhieuBanHangDaoBase).FullName);
        }
        #endregion
        /// <summary>
        /// Read data from IDataReader, convert to ChiTietPhieuBanHang Object.
        /// </summary>
        private static ChiTietPhieuBanHang GetChiTietPhieuBanHangByDataReader(IDataReader reader)
        {
            String methodName = "GetChiTietPhieuBanHangByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                ChiTietPhieuBanHang result = new ChiTietPhieuBanHang();
                //read MaPhieuBanHang field data.

                result.MaPhieuBanHang = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuBanHang");

                //read MaMatHang field data.

                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");

                //read MaDonViTinh field data.

                result.MaDonViTinh = ManagerDao.GetintDataFromDataReader(reader, "MaDonViTinh");

                //read SoLuong field data.

                result.SoLuong = ManagerDao.GetintDataFromDataReader(reader, "SoLuong");

                //read GiaBan field data.

                result.GiaBan = ManagerDao.GetdoubleDataFromDataReader(reader, "GiaBan");

                //read Order field data.

                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Define function Insert for ChiTietPhieuBanHang Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Insert(ChiTietPhieuBanHang obj)
        {
            bool result = false;
            String methodName = "InsertChiTietPhieuBanHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertChiTietPhieuBanHang");

            //Add value "MaPhieuBanHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaPhieuBanHang", DbType.Int64, obj.MaPhieuBanHang);
            //Add value "MaMatHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString, obj.MaMatHang);
            //Add value "MaDonViTinh"  paramater.
            ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32,
                ConvertValueObject.CastObjValueToInsertDB(obj.MaDonViTinh));
            //Add value "SoLuong"  paramater.
            ManagerDao.AddInParameter(cmd, "SoLuong", DbType.Int32,
                ConvertValueObject.CastObjValueToInsertDB(obj.SoLuong));
            //Add value "GiaBan"  paramater.
            ManagerDao.AddInParameter(cmd, "GiaBan", DbType.Double,
                ConvertValueObject.CastObjValueToInsertDB(obj.GiaBan));
            ManagerDao.AddInParameter(cmd, "CurencyCode", DbType.String,
              ConvertValueObject.CastObjValueToInsertDB(obj.CurencyCode));

            //Add value "Order"  paramater.
            //ManagerDao.AddInParameter(cmd, "Order", DbType.Int32,
            //	ConvertValueObject.CastObjValueToInsertDB(obj.Order));    
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Define function Update for ChiTietPhieuBanHang Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Update(ChiTietPhieuBanHang obj)
        {
            bool result = false;
            String methodName = "UpdateChiTietPhieuBanHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdateChiTietPhieuBanHang");


            //Add value "MaPhieuBanHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaPhieuBanHang", DbType.Int64, ConvertValueObject.CastObjValueToInsertDB(obj.MaPhieuBanHang));
            //Add value "MaMatHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString, ConvertValueObject.CastObjValueToInsertDB(obj.MaMatHang));
            //Add value "MaDonViTinh"  paramater.
            ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.MaDonViTinh));
            //Add value "SoLuong"  paramater.
            ManagerDao.AddInParameter(cmd, "SoLuong", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.SoLuong));
            //Add value "GiaBan"  paramater.
            ManagerDao.AddInParameter(cmd, "GiaBan", DbType.Double, ConvertValueObject.CastObjValueToInsertDB(obj.GiaBan));
            //Add value "Order"  paramater.
            ManagerDao.AddInParameter(cmd, "Order", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.Order));
            // add parameter store return value.
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (rows == 1)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }

            }
            catch (Exception e)
            {
                //write error Message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        /// <summary>
        /// Get Data ChiTietPhieuBanHang object. 
        /// </summary>
        /// <param name="maPhieuBanHang" >This is primary key to get data.</param>
        /// <param name="maMatHang" >This is primary key to get data.</param>
        /// <returns>ChiTietPhieuBanHang object.</returns>

        public static ChiTietPhieuBanHang Get(Int64 maPhieuBanHang, string maMatHang)
        {
            String methodName = "GetChiTietPhieuBanHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                ChiTietPhieuBanHang result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetChiTietPhieuBanHang");
                //Add value "MaPhieuBanHang"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaPhieuBanHang", DbType.Int64, maPhieuBanHang);
                //Add value "MaMatHang"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaMatHang", DbType.AnsiString, maMatHang);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetChiTietPhieuBanHangByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        /// <summary>
        /// Get All ChiTietPhieuBanHang object from table ChiTietPhieuBanHang.
        /// </summary>
        /// <returns>List ChiTietPhieuBanHang object.</returns>
        public static List<ChiTietPhieuBanHangResult> GetAll()
        {
            String methodName = "GetAll";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<ChiTietPhieuBanHangResult> listChiTietPhieuBanHang = new List<ChiTietPhieuBanHangResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllChiTietPhieuBanHang");

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listChiTietPhieuBanHang.Add(GetChiTietPhieuBanHangResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listChiTietPhieuBanHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }

        /// <summary>
        /// Search ChiTietPhieuBanHang object in table ChiTietPhieuBanHang with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List ChiTietPhieuBanHangResult object.</returns>
        public static List<ChiTietPhieuBanHangResult> SearchChiTietPhieuBanHangSomeCondition(ChiTietPhieuBanHang t)
        {
            String methodName = "SearchChiTietPhieuBanHangSomeCondition";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<ChiTietPhieuBanHangResult> listChiTietPhieuBanHang = new List<ChiTietPhieuBanHangResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spSearchChiTietPhieuBanHangByCondition");
                //Add value Condition  paramater.
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String, ConvertValueObject.CastObjValueToInsertDB(t.Condition));

                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, t.ObjectPaging.PageIndex);
                //Add value PageSize  paramater.
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32, t.ObjectPaging.PageSize);
                //Add value SortField  paramater.
                ManagerDao.AddInParameter(cmd, "SortField", DbType.String, t.ObjectSortOrder.SortField);
                //Add value SortExpression  paramater.
                ManagerDao.AddInParameter(cmd, "SortExpression", DbType.String, t.ObjectSortOrder.SortExpression);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listChiTietPhieuBanHang.Add(GetChiTietPhieuBanHangResultByDataReader(reader));
                        if (t.ToltalRow <= 0)
                            t.ToltalRow = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
                    }

                    reader.Close();
                }

                return listChiTietPhieuBanHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        /// <summary>
        /// Delete ChiTietPhieuBanHang object. 
        /// </summary>
        /// <param name="MaPhieuBanHang" >This is primary key to delete.</param>
        /// <param name="MaMatHang" >This is primary key to delete.</param>
        /// <returns>True/False.</returns>

        public static bool Delete(Int64 MaPhieuBanHang, string MaMatHang)
        {
            String methodName = "DeleteChiTietPhieuBanHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteChiTietPhieuBanHang");
                //Add value "MaPhieuBanHang"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaPhieuBanHang", DbType.Int64, MaPhieuBanHang);
                //Add value "MaMatHang"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaMatHang", DbType.AnsiString, MaMatHang);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to ChiTietPhieuBanHangResult Object.
        /// </summary>
        private static ChiTietPhieuBanHangResult GetChiTietPhieuBanHangResultByDataReaderByLanguage(IDataReader reader, string LanguageId)
        {
            String methodName = "GetChiTietPhieuBanHangResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                ChiTietPhieuBanHangResult result = new ChiTietPhieuBanHangResult();
                //read MaPhieuBanHang field data.
                result.MaPhieuBanHang = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuBanHang");

                //read MaMatHang field data.
                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");

                //read MaDonViTinh field data.
                result.MaDonViTinh = ManagerDao.GetintDataFromDataReader(reader, "MaDonViTinh");

                //read SoLuong field data.
                result.SoLuong = ManagerDao.GetintDataFromDataReader(reader, "SoLuong");

                //read GiaBan field data.
                result.GiaBan = ManagerDao.GetdoubleDataFromDataReader(reader, "GiaBan");

                //read Order field data.
                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to ChiTietPhieuBanHangResult Object.
        /// </summary>
        protected static ChiTietPhieuBanHangResult GetChiTietPhieuBanHangResultByDataReader(IDataReader reader)
        {
            String methodName = "GetChiTietPhieuBanHangResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                ChiTietPhieuBanHangResult result = new ChiTietPhieuBanHangResult();
                //read MaPhieuBanHang field data.
                result.MaPhieuBanHang = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuBanHang");

                //read MaMatHang field data.
                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");

                //read MaDonViTinh field data.
                result.MaDonViTinh = ManagerDao.GetintDataFromDataReader(reader, "MaDonViTinh");

                //read SoLuong field data.
                result.SoLuong = ManagerDao.GetintDataFromDataReader(reader, "SoLuong");

                //read GiaBan field data.
                result.GiaBan = ManagerDao.GetdoubleDataFromDataReader(reader, "GiaBan");

                //read Order field data.
                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }

    }
}